<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
/**
 * 微信二维码管理
 * @package App\Http\Controllers\Admin
 */
class WxQrcodeController extends BaseController
{
    private $wxQrcodeSer;
    /**
     * WxQrcodeController constructor.
     */
    public function __construct()
    {
        $this->wxQrcodeSer = new \App\Services\WxQrcodeService();
    }

    /**
     * 二维码列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxQrcodeUse::select('*');
            //关键字
            $keyword = $request->get('keyword', '');
            if ($keyword){
                $query->where('name', 'like', '%'.$keyword.'%');
            }
            //分组
            $groupId = $request->get('group_id', 0);
            if ($groupId){
                $query->where('group_id', $groupId);
            }
            $list = $query->paginate($pageSize);
            if ($list){
                $app = \App\Services\EasyWechat::officialAccount();
                foreach ($list as $k => $v){
                    $v['group'] = $v->group;
                    $v['qrcode'] = $v->qrcode;
                    $v['qrcode_url'] = $app->qrcode->url($v['qrcode']['ticket']);
                    $list[$k] = $v;
                }
            }
            return $list;
        }
        //二维码分组数据
        $groupList = (new \App\Services\WxQrcodeGroupService())->getGroupList();
        $this->viewData['groupList'] = $groupList;
        return view('admin.wxqrcode.index', $this->viewData);
    }
    /**
     * 新建分组
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminWxQrcodePost $request)
    {
        try {
            $qrcodeData = $request->toArray();
            if(!$this->wxQrcodeSer->saveQrcodeUse($qrcodeData)){
                throw new \Exception('保存失败');
            }
            return $this->showMsg('success', '新增成功', [], 'admin.sysmenugroup.index');
        }catch (\Exception $e){
            return $this->showMsg('error', $e->getMessage());
        }
    }
    /**
     * 删除
     * @param Request $request
     */
    public function destroy(Request $request)
    {
        try {
            $qrcodeId = $request->input('qrcode_id', 0);
            $this->wxQrcodeSer->releaseQrcodeUse($qrcodeId);
            return $this->showMsg('success', '删除成功', [], 'admin.sysmenugroup.index');
        }catch (\Exception $e){
            return $this->showMsg('error', '删除失败:'.$e->getMessage());
        }
    }

    /**
     * 扫码日志
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function scanlog(Request $request)
    {
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxQrcodeScanlog::select('*');
            //扫码用户openid
            $from_openid = $request->get('from_openid', '');
            if ($from_openid){
                $query->where('from_openid', $from_openid);
            }
            //二维码ID
            $qrcode_id = $request->get('qrcode_id', 0);
            if ($qrcode_id){
                $query->where('qrcode_id', $qrcode_id);
            }
            //关联业务ID
            $relation_id = $request->get('relation_id', '');
            if ($relation_id){
                $query->where('relation_id', $relation_id);
            }
            //分组
            $groupId = $request->get('group_id', 0);
            if ($groupId){
                $query->where('group_id', $groupId);
            }
            $list = $query->paginate($pageSize);
            if ($list){
                $app = \App\Services\EasyWechat::officialAccount();
                foreach ($list as $k => $v){
                    $v['group'] = $v->group;
                    $v['qrcode'] = $v->qrcode;
                    $v['qrcode_url'] = $app->qrcode->url($v['qrcode']['ticket']);
                    $list[$k] = $v;
                }
            }
            return $list;
        }
        //二维码分组数据
        $groupList = (new \App\Services\WxQrcodeGroupService())->getGroupList();
        $this->viewData['groupList'] = $groupList;
        return view('admin.wxqrcode.scanlog', $this->viewData);
    }
}
